package gui;

import player.MyPlayer;
import player.MyPlayerThread;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PlayerGui extends MyFrame {

    private String songList[] = {"./music/断桥残雪.mp3",
            "./music/万茜-认真地老去.mp3",
            "./music/天外来物.mp3",
            "./music/日不落.mp3",
             "./music/玫瑰花的葬礼.mp3",
             "./music/小螺号.mp3",
              "./music/白羊.mp3",
               "./music/让我们荡起双桨.mp3"};

    //面板定义
    private JPanel playPanel;
    private static BlankPanel showPlayPanel;

    //定义按钮等相关组件
    ImageButton bntStart, bntStop, bntNext, bntPause, bntPrev;

    //定义播放器
    MyPlayer player;
    MyPlayerThread playerThread;

    //监听器定义
    PlayOperatorListener playOperatorListener;

    public PlayerGui() {
        //初始化图形界面相关组件
        init();

        //非线程化音乐播放器
        player = new MyPlayer(songList);
        //线程化音乐播放器
        playerThread = new MyPlayerThread(songList);

        //添加监听事件
        addActionListener();
    }

    //播放面板初始化
    public void init() {
        //设置主窗口布局
        getContentPane().setLayout(null);

        //面板实例化
        playPanel = new BlankPanel();
        playPanel.setSize(1000, 150);
        showPlayPanel = new BlankPanel();

        //面板设置
        playPanel.setLayout(new BorderLayout());
        showPlayPanel.setLayout(new FlowLayout());

        //组件实例化
        bntStart = new ImageButton("./drawable/icon/play.png");
        bntNext = new ImageButton("./drawable/icon/next.png");
        bntPrev = new ImageButton("./drawable/icon/prev.png");
        bntPause = new ImageButton("./drawable/icon/pause.png");
        bntStop = new ImageButton("./drawable/icon/stop.png");


        //添加组件
        showPlayPanel.add(bntPrev);
        showPlayPanel.add(bntPause);
        showPlayPanel.add(bntStart);
        showPlayPanel.add(bntNext);
        showPlayPanel.add(bntStop);

        //默认设置暂停按钮不可见，播放按钮可见
        bntPause.setVisible(false);


        //监听器实例化
        playOperatorListener = new PlayOperatorListener();

        //添加面板
        getContentPane().add(playPanel);
        playPanel.setLocation(0, 450);
        playPanel.add(showPlayPanel, BorderLayout.CENTER);


    }

    public void addActionListener() {
        //播放组件监听事件
        bntStart.addActionListener(playOperatorListener);
        bntNext.addActionListener(playOperatorListener);
        bntPrev.addActionListener(playOperatorListener);
        bntStop.addActionListener(playOperatorListener);
    }

    class PlayOperatorListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == bntStart) {
                //player.playList();
                playerThread.start();
                bntStart.setVisible(true);
                bntPause.setVisible(true);

            }
            if(e.getSource()==bntPrev){
                playerThread.isPrev=true;
                playerThread.pause();
                bntPrev.setVisible(true);

            }
            else if(e.getSource()==null){
                playerThread.run();
                bntPause.setVisible(true);





            }
            if (e.getSource()==bntNext) {
                playerThread.pause();
                bntNext.setVisible(true);
                bntPause.setVisible(false);

            }
            if (e.getSource()==bntPrev) {
                playerThread.pause();
                bntPrev.setVisible(true);


            }
            if (e.getSource()==bntStop) {
                playerThread.pause();
                bntStop.setVisible(true);


            }




        }
    }
}

